﻿using UnityEngine;
using System.Collections;

public class obstacleLink : MonoBehaviour {
	
	public GameObject[] obstacles = new GameObject[2];
	
	private int randomNum = 0;

	public bool oneFourSplit;
	public bool threeFourSplit;
	public bool fiftyFiftySplit;

	public bool show;
	
	public float waitTime;

	private int a;

	public ButtonLine line;
	
	// Use this for initialization
	void Start () {

		if(show == false)
		{
			obstacles[0].SetActive(false);
			obstacles[1].SetActive(false);
		}

		line.obstacles = new GameObject[line.a+2];
		line.obstacles[line.a] = obstacles [0];
		line.a++;
		line.obstacles[line.a] = obstacles [1];
		line.a++;
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	public void randomDoor ()
	{
		if(oneFourSplit == true)
		{
			randomNum = Random.Range(1,4);
			if(randomNum == 1)
			{
				a=0;
				activateObstacle();
			}
			else
			{
				a=1;
				activateObstacle();
			}
		}
		if(threeFourSplit == true)
		{
			randomNum = Random.Range(1,4);
			if(randomNum <= 3)
			{
				a=0;
				activateObstacle();
			}
			else
			{
				a=1;
				activateObstacle();
			}
		}
		if(fiftyFiftySplit == true)
		{
			randomNum = Random.Range(1,4);
			if(randomNum <= 2)
			{
				a=0;
				activateObstacle();
			}
			else
			{
				a=1;
				activateObstacle();
			}
		}
	}

	public void activateObstacle()
	{
		Debug.Log ("start couroutine");
		StartCoroutine (runObstacle());
	}
	
	IEnumerator runObstacle()
	{
		Debug.Log ("ienumerator running");
		showObstacle ();
		Debug.DrawLine(transform.position,obstacles[a].transform.position,Color.red,duration:20f);
		yield return new WaitForSeconds(waitTime);
		showObstacle ();
	}

	public void showObstacle ()
	{
		if(show == true)
		{
			
			show = false;
			obstacles[a].SetActive(false);
			line.target = obstacles[a];
			line.lineOn=true;
		}
		else
		{
			show = true;
			obstacles[a].SetActive(true);
			line.target = obstacles[a];
			line.lineOn=true;
		}
	}
}
